#include <unistd.h>
#include <stdlib.h>

/* definition des structures */
typedef struct s_block	*cell;

struct s_block
{
	int				data;
	cell			next;
};

typedef struct s_pool	*pool;

struct s_pool
{
	cell			tab;
	size_t			index, size;
	cell			free;
};

/* definition des fonctions */
pool make (size_t max)
{
	pool 			p = NULL;

	p = malloc (sizeof(struct s_pool));
	p->tab = malloc (max * sizeof(struct s_block));		
	p->size = max;
	p->index = 0;
	p->free = NULL;

	return p;
}

cell alloc (pool p)
{
	cell 			c = NULL;

	if (p->free)
	{
		c = p->free;
		p->free = c->next;
	}
	else
	{
		if (p->index < p->size)
		{
			c = p->tab + p->index;
			p-> index += 1;
		}
	}
	c->next = NULL;

	return c;
}

void release (cell c, pool p)
{
	c->next = p->free;
	p->free = c;
}

void reset (pool p)
{
	p->index = 0;
	p->free = NULL;
}


int main ()
{
	pool		p = NULL;
	cell		c1 = NULL, c2 = NULL;

	p = make (10);
	c1 = alloc (p);
	c2 = alloc (p);
	release (c1, p);
	c1 = alloc (p);
	reset (p);

	return 0;
}

